import java.util.Arrays;

/**
 * 供暖器
 * LeetCode 475
 */

public class test6 {

    /**
     * 判断当前供暖器和房子之间距离是否为最优
     * 房子与当前供暖器之间距离 < 房子与下一个供暖器之间的距离 则为最优
     */
    public static boolean best(int[] houses, int[] heaters,int i,int j){
        return j == heaters.length -1||
                Math.abs(heaters[j] - houses[i]) < Math.abs(heaters[j+1]-houses[i]);
    }

    public int findRadius(int[] houses, int[] heaters) {
        // 先对houses和heaters进行排序
        Arrays.sort(houses);
        Arrays.sort(heaters);
        int res =0;
        for(int i =0,j=0;i<houses.length;i++){
            while(!best(houses,heaters,i,j)){
                j++;
            }
            res= Math.max(res,Math.abs(heaters[j]-houses[i]));
        }
        return res;
    }
}
